package com.matt.components.intercepter;

import com.matt.commons.exception.BusinessException;
import com.matt.commons.utils.JsonUtil;
import com.matt.commons.utils.RemoteCallUtil;
import com.matt.foundation.constant.CommonConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.utils.ModelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.matt.commons.constant.CommonConstant.*;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/7/25
 * *******************************
 */
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ticket = request.getHeader(TICKET);
        if(StringUtils.isEmpty(ticket)){
            String auth = request.getHeader(AUTH_KEY);
            String platform = request.getHeader(PLATFORM_KEY);
            String reqUUID = request.getHeader(REQ_UUID_KEY);
            String compVer = request.getHeader(COMP_VERSION_KEY);
            String url = request.getRequestURL().toString();


//            if(StringUtils.isEmpty(auth) || StringUtils.isEmpty(platform) ||
//                    StringUtils.isEmpty(reqUUID) || StringUtils.isEmpty(compVer) ){
//                throw new BusinessException(ErrorCode.UNDEFINED, "你无权访问改系统！");
//            }
//
//            String perferSign = RemoteCallUtil.authSigh(url, compVer, reqUUID, platform);
//            if(!perferSign.equals(auth)){
//                throw new BusinessException(ErrorCode.UNDEFINED, "你无权访问改系统！！");
//            }
        }
        String requestUrl = request.getRequestURI();
        String params = JsonUtil.toJson(request.getParameterMap());
        log.info("request URL:[{}] ,params:[{}]", requestUrl, params);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
